package com.ruoyi.project.modules.questionInfo.service.impl;

import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.project.modules.fileInfo.domain.FileInf;
import com.ruoyi.project.modules.fileInfo.mapper.FileInfoMapper;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.modules.questionInfo.mapper.QuestionInfoMapper;
import com.ruoyi.project.modules.questionInfo.domain.QuestionInfo;
import com.ruoyi.project.modules.questionInfo.service.IQuestionInfoService;
import com.ruoyi.common.core.text.Convert;

/**
 * 问题信息Service业务层处理
 *
 * @author smallrain
 * @date 2021-12-06
 */
@Service
@AllArgsConstructor
public class QuestionInfoServiceImpl implements IQuestionInfoService {

    private QuestionInfoMapper questionInfoMapper;

    private FileInfoMapper fileInfoMapper;

    /**
     * 查询问题信息
     *
     * @param id 问题信息主键
     * @return 问题信息
     */
    @Override
    public QuestionInfo selectQuestionInfoById(Long id) {
        return questionInfoMapper.selectQuestionInfoById(id);
    }

    /**
     * 查询问题信息列表
     *
     * @param questionInfo 问题信息
     * @return 问题信息
     */
    @Override
    public List<QuestionInfo> selectQuestionInfoList(QuestionInfo questionInfo) {
        return questionInfoMapper.selectQuestionInfoList(questionInfo);
    }

    /**
     * 新增问题信息
     *
     * @param questionInfo 问题信息
     * @return 结果
     */
    @Override
    public int insertQuestionInfo(QuestionInfo questionInfo) {
        questionInfo.setCreateTime(DateUtils.getNowDate());
        questionInfo.setCreateBy(ShiroUtils.getLoginName());
        int i = questionInfoMapper.insertQuestionInfo(questionInfo);
        if (CollectionUtil.isNotEmpty(questionInfo.getFileInfList())) {
            for (FileInf fileInf : questionInfo.getFileInfList()) {
                fileInf.setObjectType("3");
                fileInf.setObjectId(questionInfo.getId());
            }
            this.fileInfoMapper.batchSave(questionInfo.getFileInfList());
        }
        return i;
    }

    /**
     * 修改问题信息
     *
     * @param questionInfo 问题信息
     * @return 结果
     */
    @Override
    public int updateQuestionInfo(QuestionInfo questionInfo) {
        questionInfo.setUpdateTime(DateUtils.getNowDate());
        return questionInfoMapper.updateQuestionInfo(questionInfo);
    }

    /**
     * 批量删除问题信息
     *
     * @param ids 需要删除的问题信息主键
     * @return 结果
     */
    @Override
    public int deleteQuestionInfoByIds(String ids) {
        return questionInfoMapper.deleteQuestionInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除问题信息信息
     *
     * @param id 问题信息主键
     * @return 结果
     */
    @Override
    public int deleteQuestionInfoById(Long id) {
        this.fileInfoMapper.deleteFileInfoObjectIdAndObjectType(id, "3");
        return questionInfoMapper.deleteQuestionInfoById(id);
    }
}
